package com.splic.api.controller.product;

import com.splic.common.config.RuoYiConfig;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.utils.OssUtils;
import com.splic.common.utils.file.FileUploadUtils;
import com.splic.common.utils.uuid.IdUtils;
import com.splic.product.dto.FileUploadResultDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 通用
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/common")
public class ApiCommonController
{
    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    @ResponseBody
    public ResponseObj<FileUploadResultDto> uploadFile(MultipartFile file)
    {
        try
        {
            // 上传文件路径
            /*String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = Global.getImageHost() + fileName;*/
            String filePath =RuoYiConfig.getOssUploadPath()+ FileUploadUtils.extractFilename(file);
            String url = OssUtils.uploadFile(filePath,file.getInputStream());
            FileUploadResultDto ajax = new FileUploadResultDto();
            ajax.setFileName(filePath);
            ajax.setUrl(url);
            return ResponseObj.SUCCESS(ajax);
        }
        catch (Exception e)
        {
            return ResponseObj.ERROR("上传文件失败");
        }
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/replaceUpload")
    @ResponseBody
    public ResponseObj replaceUpload(String orderNo)
    {
        try
        {
            /*List<WeatherIcon> weatherIconList = weatherIconService.selectPageList(null);
            for(WeatherIcon weatherIcon:weatherIconList){
                InputStream inputStream = new URL(weatherIcon.getIcon()).openStream();
                String folderName = "product/2022/04/07/"+ IdUtils.fastUUID() + ".jpg";
                String url = OssUtils.uploadFile(folderName,inputStream);
                weatherIcon.setIcon(url);
                weatherIconService.update(weatherIcon);
            }*/
            return ResponseObj.SUCCESS();
        }
        catch (Exception e)
        {
            return ResponseObj.ERROR("上传文件失败");
        }
    }
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\Users\\admin\\Desktop\\easyjoy\\用户系统\\天气图标-20220407\\天气");
        for (File f:file.listFiles()){
            String folderName = "product/2022/04/07/"+ IdUtils.fastUUID().replace("-","") + ".png";
            String url = OssUtils.uploadFile(folderName,f);
            System.out.println("当前天气类型:"+f.getName()+"----当前名称:"+url+";");
        }

    }

}
